package com.adapter;


import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dstats.R;
import com.entities.News;
import com.fetch.ImageLoader;


public class NewsAdapter extends BaseAdapter {

	private List<News> news;
	private LayoutInflater mInflater;
	private boolean isYouTube;
	private static ImageLoader imageLoader;
	private Context mContext;
	static class ViewHolder{
		TextView title;
		TextView author;
		ImageView img;
		TextView date;
	}
	
	public NewsAdapter(Context context, List<News> news){
		mInflater = LayoutInflater.from(context);
		this.news = news;
		isYouTube = false;
		if(imageLoader==null)
		imageLoader = new ImageLoader(context);
		mContext = context;
	}
	
	public NewsAdapter(Context context, List<News> news, boolean isYouTube){
		mInflater = LayoutInflater.from(context);
		this.news = news;
		this.isYouTube = isYouTube;
		if(imageLoader==null)
		imageLoader = new ImageLoader(context);
		mContext = context;
	}

	
	@Override
	public int getCount() {
		return news.size();
	}

	@Override
	public News getItem(int id) {
		return news.get(id);
	}

	@Override
	public long getItemId(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.news_layout, null);
			
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.textViewNewsTitle);
			holder.author = (TextView) convertView.findViewById(R.id.textViewAuthor);
			holder.img = (ImageView) convertView.findViewById(R.id.imageViewNewsImage);
			holder.date = (TextView) convertView.findViewById(R.id.textViewVideoSentTimeAgo);
			convertView.setTag(holder);
			
		}else{
			holder = (ViewHolder) convertView.getTag();
		}

		News mNew = news.get(position);
		holder.title.setText(mNew.getTtitle());
		holder.title.setTextColor(Color.BLACK);
		holder.date.setText(getTimeDiff(mNew.getDate()));
		if(isYouTube){
			   
			   
			imageLoader.DisplayImage("http://img.youtube.com/vi/" + mNew.getLink().split("=")[1].split("&")[0] + "/hqdefault.jpg", holder.img);
			holder.author.setText(mNew.getAuthor());
		}
		return convertView;
	}
	
	private String getTimeDiff(Date date) {
		Date curDate = new Date();
		
		long diff = curDate.getTime() - date.getTime();
		long mins = diff/60000;
		long hours = mins/60;
		long days = hours/24;
		long months = days/365/12;
		long years = months /12;
		Resources r = mContext.getResources();
		String toPut;
		if(years>=1){
			if(years==1) toPut = String.valueOf(years) + " " + r.getString(R.string.year_ago);
			else toPut = String.valueOf(years) + " " + r.getString(R.string.years_ago);
		} else if (months>=1){
			if(months==1) toPut = String.valueOf(months) + " " + r.getString(R.string.month_ago); 
			else toPut = String.valueOf(months) + " " + r.getString(R.string.months_ago);
		} else if (days>=1) {
			if(days==1) toPut = String.valueOf(days) + " " + r.getString(R.string.day_ago);
			else toPut = String.valueOf(days) + " " + r.getString(R.string.days_ago);
		} else if (hours>=1){
			if(hours==1)  toPut = String.valueOf(hours) + " " + r.getString(R.string.hour_ago);
			else toPut = String.valueOf(hours) + " " + r.getString(R.string.hours_ago);
		} else {
			if(mins==1 || mins ==0) toPut = String.valueOf(mins) + " " + r.getString(R.string.minute_ago);
			else toPut = String.valueOf(mins) + " " + r.getString(R.string.minutes_ago);
		}
		return toPut;
	}

}
